💯 solving-algo | March 29, 2021
N*M
이루어진 맵에서 괴물들이 있는 위치(1)
로 표시된 곳을 피해 최소의 움직임을 카운트 해 출력하는 문제
from collections import deque
n, m = map(int, input().split())
arr = []
for _ in range(n):
arr.append(list(map(int, input())))
# 상, 하, 좌, 우
dx = [-1, 1, 0, 0] # row
dy = [0, 0, -1, 1] # col
step_count = 0
def bfs(x, y):
queue = deque()
queue.append((x, y))
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
# 범위를 초과하면 넘긴다.
if nx < 0 or ny < 0 or nx >= n or ny >= m:
continue
# 괴물이 있다면 넘긴다.
if arr[nx][ny] == 0:
continue
# 해당 노드를 처음 방문하는 경우에만, 그 전 노드 값 + 1
if arr[nx][ny] == 1:
arr[nx][ny] = arr[x][y] + 1
queue.append((nx, ny))
return arr[n - 1][m - 1]
print(bfs(0, 0))
+1
를 해줘서 카운트를 진행한다.